<!-- AddApproval.vue -->
<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="请求信息" prop="requestInfo">
        <el-input v-model="form.requestInfo" type="textarea" placeholder="请输入内容" />
      </el-form-item>
      <el-form-item label="使用时间" prop="requestDate">
        <el-date-picker
          clearable
          v-model="form.requestDate"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择使用时间"
          :picker-options="pickerOptions"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="地点" prop="location">
        <el-input v-model="form.location" placeholder="请输入地点" />
      </el-form-item>
      <el-form-item label="车辆信息" prop="sysVehicles">
        <el-button type="primary" @click="openSelectVehicleDialog">选择车辆</el-button>
        <el-table :data="form.sysVehicles" border style="width: 100%">
          <el-table-column label="车牌号" align="center" prop="licensePlate" />
          <el-table-column label="品牌" align="center" prop="brand" />
          <el-table-column label="购车年份" align="center" prop="purchaseYear" width="180" />
          <el-table-column label="单位名称" align="center" prop="organizationName" />
          <el-table-column label="车辆类型" align="center" prop="type" />
          <el-table-column label="座位数" align="center" prop="seatCount" />
          <el-table-column label="驾驶员" align="center" prop="driver" />
          <el-table-column label="图片" align="center" width="120">
            <template slot-scope="scope">
              <el-image
                style="width: 100px; height: 100px"
                :src="scope.row.image"
                fit="cover"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
    <!-- 选择车辆对话框 -->
    <SelectVehicleDialog :open.sync="selectVehicleDialogOpen" @submit="handleVehicleSelection" />
  </div>
</template>

<script>
import SelectVehicleDialog from "@/views/vehicles/vehicles/SelectVehicleDialog.vue";
import { addApproval } from "@/api/approval/approval";

export default {
  name: "AddApproval",
  components: {
    SelectVehicleDialog
  },
  props: {
    open: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      selectVehicleDialogOpen: false,
      form: {
        sysVehicles: [],
        requestInfo: null,
        requestDate: null,
        location: null,
        driver: null
      },
      rules: {
        sysVehicles: [
          { required: true, message: '请选择车辆', trigger: 'change' }
        ],
        requestInfo: [
          { required: true, message: '请输入请求信息', trigger: 'blur' }
        ],
        requestDate: [
          { required: true, message: '请选择使用时间', trigger: 'change' }
        ],
        location: [
          { required: true, message: '请输入地点', trigger: 'blur' }
        ]
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() <= Date.now(); // 禁用当前日期及之前的日期
        }
      }
    };
  },
  methods: {
    handleVehicleSelection(selectedVehicles) {
      this.form.sysVehicles = selectedVehicles.map(vehicle => ({
        id: vehicle.id,
        licensePlate: vehicle.licensePlate,
        brand: vehicle.brand,
        purchaseYear: vehicle.purchaseYear,
        organizationName: vehicle.organizationName,
        type: vehicle.type,
        seatCount: vehicle.seatCount,
        driver: vehicle.driver,
        image: vehicle.image // 添加 image 字段
      }));
    },
    openSelectVehicleDialog() {
      this.selectVehicleDialogOpen = true;
    },
    // 取消按钮
    cancel() {
      this.$emit("update:open", false);
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sysVehicles: [],
        requestInfo: null,
        requestDate: null,
        location: null,
        driver: null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log('Form Data:', this.form);
          addApproval(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.$emit("update:open", false);
            this.$emit("submit");
          });
        }
      });
    },
  }
};
</script>
